import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
//引入svg需要用到插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
//mock插件提供方法
import { viteMockServe } from 'vite-plugin-mock'

export default defineConfig(({ command, mode }) => {
  //获取各种环境下的对应的变量
  const env = loadEnv(mode, process.cwd())
  console.log(env.VITE_SERVE, env.VITE_APP_BASE_API)
  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
      viteMockServe({
        localEnabled: command === 'serve', //保证开发阶段可以使用mock接口
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve('./src'), // 相对路径别名配置，使用 @ 代替 src
      },
    },
    //scss全局变量一个配置
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },
    },
    //代理跨域
    //代理跨域
    server: {
      proxy: {
        // 添加快手API的代理规则
        '/kuaishou-api': {
          target: 'http://192.168.0.111:3006',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/kuaishou-api/, ''),
        },
        // 添加抖音API的代理规则
        '/douyin-api': {
          target: 'http://192.168.0.111:3006',  
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/douyin-api/, ''),
        },
        // 添加小红书API的代理规则
        '/xiaohongshu-api': {
          target: 'http://192.168.0.111:3006',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/xiaohongshu-api/, ''),
        },
        // 添加GLM-4.5专用代理规则
        '/glm-api': {
          target: 'http://192.168.0.111:3006', // 根据后端实际地址调整

          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/glm-api/, '/api/v1/admin/glm-4.5'), // 根据后端实际路径调整
        },
        // 原有API代理规则
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  }
})